package com.cardone.platform.usercenter.dao;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.usercenter.dto.OrgDto;
import com.google.common.collect.Maps;

/**
 * 组织
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class OrgJdbcDao implements OrgDao {
	@Override
	public int deleteByIds(final OrgDto deleteOrg) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteOrg.getAttrs())) {
			model.putAll(deleteOrg.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteOrg.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final OrgDto findOrg) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findOrg, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findOrg, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findOrg.getAttrs())) {
			model.putAll(findOrg.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final OrgDto findOrg) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findOrg);

		final Model model = ModelUtils.newModel(new ModelArgs(findOrg, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findOrg.getAttrs())) {
			model.putAll(findOrg.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> List<P> findListByIdForTree(final Class<P> mappedClass, final String id, final boolean isSuperRole) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(Attributes.id.name(), id);
		model.put("isSuperRole", isSuperRole);

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByIdForTree.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final OrgDto findListOrg) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListOrg).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListOrg.getAttrs())) {
			model.putAll(findListOrg.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public int insertByCode(final OrgDto insertOrg) {
		if (StringUtils.isBlank(insertOrg.getId())) {
			insertOrg.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertOrg));

		if (MapUtils.isNotEmpty(insertOrg.getAttrs())) {
			model.putAll(insertOrg.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final OrgDto paginationOrg) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationOrg).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationOrg.getAttrs())) {
			model.putAll(paginationOrg.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationOrg), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final OrgDto readOrg) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readOrg, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readOrg.getAttrs())) {
			model.putAll(readOrg.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readOrg);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readOrg, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final OrgDto saveOrg) {
		final P oldOrg = this.findByCodeOrId(mappedClass, saveOrg);

		if (oldOrg == null) {
			this.insertByCode(saveOrg);

			return this.findByCodeOrId(mappedClass, saveOrg);
		}

		final int updateCount = this.updateByCode(saveOrg, oldOrg);

		if (updateCount < 1) {
			return oldOrg;
		}

		return this.findByCodeOrId(mappedClass, saveOrg);
	}

	@Override
	public int updateByCode(final OrgDto updateOrg) {
		final OrgDto oldOrg = this.findByCodeOrId(OrgDto.class, updateOrg);

		return this.updateByCode(updateOrg, oldOrg);
	}

	private <P> int updateByCode(final OrgDto updateOrg, final P oldOrg) {
		if (oldOrg == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.typeId.name(), Attributes.code.name(), Attributes.name.name(), Attributes.value.name(), Attributes.remark.name() }, updateOrg);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateOrg, oldOrg, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldOrg, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldOrg, updateOrg, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateOrg, updateProperties), new ModelArgs(updateOrg, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateOrg.getAttrs())) {
			model.putAll(updateOrg.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}